package com.cb.myzhxy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cb.myzhxy.pojo.Admin;
import com.cb.myzhxy.service.AdminService;
import com.cb.myzhxy.util.MD5;
import com.cb.myzhxy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "系统管理员控制器")
@RestController
@RequestMapping("/sms/adminController")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 分页获取所有Admin信息
     */
    @ApiOperation("分页获取所有Admin信息【带条件】")
    @GetMapping("/getAllAdmin/{pageNo}/{pageSize}")
    public Result getAllAdmin(@ApiParam("分页查询页码数") @PathVariable(value = "pageNo") Integer pageNo,
                              @ApiParam("分页查询页大小") @PathVariable(value = "pageSize") Integer pageSize,
                              @ApiParam("分页查询模糊匹配班级名") @RequestParam(required = false) String adminName){
        Page<Admin> page = new Page<>(pageNo, pageSize);
        IPage<Admin> page1=adminService.getAdmins(page,adminName);
        return Result.ok(page1);
    }

    /**
     * 添加或修改Admin信息
     */
    @ApiOperation("添加或修改Admin信息")
    @PostMapping("/saveOrUpdateAdmin")
    public Result saveOrUpdateAdmin(@RequestBody Admin admin){
        if (null==admin.getId()||0==admin.getId()) {
            admin.setPassword(MD5.encrypt(admin.getPassword()));
        }
        adminService.saveOrUpdate(admin);
        return Result.ok();
    }

    /**
     * 删除Admin信息
     */
    @ApiOperation("删除Admin信息")
    @DeleteMapping("/deleteAdmin")
    public Result deleteAdmin(@RequestBody List<Integer> ids){
        adminService.removeByIds(ids);
        return Result.ok();
    }
}
